package tk.xboot.ws.controller;


import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import javax.annotation.Resource;


/**
 * A nice feature of the Spring Test support is that the application context is cached
 * in between tests, so if you have multiple methods in a test case, or multiple test cases
 * with the same configuration, they only incur the cost of starting the application once.
 * You can control the cache using the @DirtiesContext annotation.
 *
 *
 *
 *  the full Spring application context is started, but without the server.
 * Note:
 * @SpringBootTest
 *      Only the full Spring application context is started, but without the server.
 *
 * @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
 *      The full Spring application context is started, and the server started on random port.
 *      to get the port, you can using @LocalServerPort to a int field.
 *
 * visit https://spring.io/guides/gs/testing-web/ for more details of how to test web application
 * in spring boot.
 *
 *
 * ATTENTION:
 *  @see tk.xboot.ws.conf.WebSocketConf @Profile("!test")
 *
 */

@ActiveProfiles({"test"})
@RunWith(SpringRunner.class)
@SpringBootTest
public class I18nControllerTest1 {

    private MockMvc mvc;

    @Resource
    private WebApplicationContext context;

    @Before
    public void before() {
        mvc = MockMvcBuilders.webAppContextSetup(context).build();
    }

    @Test
    public void testHello() throws Exception {
        String enResult = mvc.perform(MockMvcRequestBuilders.post("/hello?lang=en_US"))
                .andExpect(MockMvcResultMatchers.status().isOk()).andReturn().getResponse().getContentAsString();

        String zhResult = mvc.perform(MockMvcRequestBuilders.post("/hello?lang=zh_CN"))
                .andExpect(MockMvcResultMatchers.status().isOk()).andReturn().getResponse().getContentAsString();

        System.out.println("enResult : " + enResult);
        System.out.println("zhResult : " + zhResult);
    }
}
